package cn.edu.cug.cs.gtl.oi.ui.actions;

import cn.edu.cug.cs.gtl.oi.ui.MainApp;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class SaveH3DFileActionListener implements ActionListener {
    MainApp theApp;

    public SaveH3DFileActionListener(MainApp theApp) {
        this.theApp = theApp;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser(".");//指定默认打开的本地磁盘路径
        FileNameExtensionFilter filter = new FileNameExtensionFilter("H3D文件(*.h3d)", "h3d");
        chooser.setFileFilter(filter);
        if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();   //获取用户选中的文件路径
            try {
                OutputStream outputStream = new FileOutputStream(file + ".h3d");
                outputStream.write(theApp.getProject().toByteArray());   //将数据写入输出流
                outputStream.close();   //关闭输出流
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
//            SoWriteAction writeAction = new SoWriteAction();
//            writeAction.getOutput().openFile(file + ".h3d");
//            writeAction.apply(theApp.getMainWindow().getRenderPanel().getSceneGraph());
//            writeAction.getOutput().closeFile();
        }
    }
}
